package com.woniu.utils;

import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

@Component

/**
 * 反射工具类
 */
public class ReflectUtils {


	/**
	 * 将一个对象转化为 属性的map集合
	 * @param o 要转化的对象
	 * @return
	 */
	public static Map<String, Object> getFieldsMap(Object o){
		HashMap<String, Object> hm = new HashMap<String, Object>();
		Field[] Fields = o.getClass().getDeclaredFields();
		try {
			for (Field field : Fields) {
				if (!field.getName().equals("serialVersionUID")) {
					field.setAccessible(true);
					if(field.get(o)!=null) {
						hm.put(field.getName(), field.get(o));
					}
				}
			}
		} catch (Exception e) {
			e.getMessage();
		}
		return hm;
	}

	public static ArrayList<Entry<String, Object>> EntryList(Object o){
		return new ArrayList<Entry<String,Object>>(getFieldsMap(o).entrySet());
	}
}
